package com.zyh.flower;

import java.util.Scanner;

public class Crud {
    public static final Scanner INPUT = FlowerIndex.INPUT;
    public static final FlowerOrder[] orderList = FlowerIndex.orderList;

    public static void index() {
        outer:
        while (true) {
            System.out.println("==============================欢迎来到\"七彩鲜花\"商城=========================================");
            System.out.println("1.查询所有销售订单");
            System.out.println("2.修改销售订单");
            System.out.println("3.删除销售订单");
            System.out.println("4.增加销售订单");
            System.out.println("5.退出系统");
            System.out.println("============================================================================================");
            System.out.println("请输入您的选择：");
            int choice = INPUT.nextInt();
            switch (choice) {
                case 1://查询时所有订单功能 findAll()
                    findAll();
                    break;
                case 2://修改订单功能 update()
                    update();
                    break;
                case 3://删除订单功能 delete()
                    delete();
                    break;
                case 4:
                    add();
                    break;
                default:
                    System.err.println("系统退出！");
                    break outer;
            }
        }
    }


    //添加订单功能
    /*
    1.用户输入订单编号
    2.查询编号是否已经存在
    3.不存在则创建新数组，长度为原数组长度加1
    4.用户输入对应的数据
    5.将输入的新数据放到一个对象中
    6.将新对象存入数组最后一位
    7.根据订单编号进行冒泡排序从小到大展示
     */
    public static void add() {
        System.out.println("请输入您要新添加的订单编号：");
        String addId = INPUT.next();
        for (int i = 0; i < orderList.length; i++) {
            if (orderList[i].getId().equals(addId)) {//说明订单编号已经存在
                System.out.println("您输入的订单编号已经存在，请重新输入：");
                return;
            }
        }
        //走到此处说明输入的编号为新的编号
        String newName = null;
        int newNumber = 0;
        double newPrice = 0;
        String newDate = null;
        String newSalEmp = null;
        String newBeiZhu = null;
        System.out.println("请输入新订单的鲜花名称：");
        newName = INPUT.next();
        //完善功能
        while (true) {
            System.out.println("请输入新订单的销售数量：");
            newNumber = INPUT.nextInt();
            if (newNumber < 0) {
                System.err.println("输入有误！");
                continue;
            } else {
                break;
            }
        }
        while (true) {
            System.out.println("请输入新订单的鲜花价格：");
            newPrice = INPUT.nextDouble();
            if (newPrice < 0) {
                System.err.println("输入有误！");
                continue;
            } else {
                break;
            }
        }
        System.out.println("请输入新订单的销售日期：");
        newDate = INPUT.next();
        System.out.println("请输入新订单的销售员：");
        newSalEmp = INPUT.next();
        System.out.println("请输入新订单的备注：");
        newBeiZhu = INPUT.next();
        FlowerOrder[] newOrder = new FlowerOrder[6];
        FlowerOrder order = new FlowerOrder(addId, newName, newNumber, newPrice, newDate, newSalEmp, newBeiZhu);
        System.out.println("您确定要添加吗？（y/n）");
        String confirm = INPUT.next();
        if ("y".equals(confirm)) {
            //创建长度为6的数组
            for (int i = 0; i < newOrder.length - 1; i++) {
                newOrder[i] = orderList[i];
                newOrder[5] = order;
            }
        }
        printHead();
        for (int i = 0; i < newOrder.length; i++) {
            System.out.println(newOrder[i].getId() + "\t" + newOrder[i].getName() + "\t" + newOrder[i].getNumber() + "\t\t" + newOrder[i].getPrice() + "\t" + newOrder[i].getDate() + "\t" + newOrder[i].getSalEpy() + "\t" + newOrder[i].getBeiZhu());
        }
    }

    //删除订单功能
    public static void delete() {
        System.out.println("请输入要删除的订单编号：");
        String deleteId = INPUT.next();
        //假设输入错误的编号
        boolean flag = false;
        for (int i = 0; i < orderList.length; i++) {
            //根据输入的编号来查询是否存在该编号
            if (orderList[i].getId().equals(deleteId)) {
                //存在则改为true并结束遍历数组
                flag = true;
                break;
            }
        }
        //循环走完后flag的值没修改，说明数组中不存在输入的编号
        if (flag == false) {
            System.err.println("对不起，没有您要删除的销售编号！");
            return;
        }
        System.out.println();
        //走到此处说明没有执行上面if里面的break，说明输入的编号是对的
        System.out.println("您要删除的订单详细信息如下：");
        printHead();
        for (int i = 0; i < orderList.length; i++) {
            if (orderList[i].getId().equals(deleteId)) {
                System.out.println(orderList[i].getId() + "\t" + orderList[i].getName() + "\t" + orderList[i].getNumber() + "\t\t" + orderList[i].getPrice() + "\t" + orderList[i].getDate() + "\t" + orderList[i].getSalEpy() + "\t" + orderList[i].getBeiZhu());
            }
        }
        System.out.println("确认要删除吗？（y/n）");
        String confirm = INPUT.next();
        if ("y".equals(confirm)) {
            //删除这条数据
            //创建新数组
            FlowerOrder[] newOrder = new FlowerOrder[4];
            int index = 0;
            for (int i = 0; i < orderList.length; i++) {
                if (orderList[i].getId().equals(deleteId)) {
                    continue;
                } else {
                    newOrder[index++] = orderList[i];
                }
                if (index == newOrder.length) {
                    break;
                }
            }
            System.out.println("删除成功！");
            printHead();
            for (int i = 0; i < newOrder.length; i++) {
                System.out.println(newOrder[i].getId() + "\t" + newOrder[i].getName() + "\t" + newOrder[i].getNumber() + "\t\t" + newOrder[i].getPrice() + "\t" + newOrder[i].getDate() + "\t" + newOrder[i].getSalEpy() + "\t" + newOrder[i].getBeiZhu());
            }
        }
    }

    //修改订单功能
    public static void update() {
        System.out.println("请输入销售编号：");
        String uid = INPUT.next();
        //判断用户输入的uid是否能在数组中找到
        boolean flag = false;
        for (int i = 0; i < orderList.length; i++) {
            if (orderList[i].getId().equals(uid)) {
                flag = true;
                break;
            }
        }
        if (flag == false) {
            System.err.println("对不起，您要修改的订单号不存在！");
            return;
        }
        System.out.println("您要修改的订单详细信息如下：");
        System.out.println();
        //打印表头
        printHead();
        for (int i = 0; i < orderList.length; i++) {
            if (orderList[i].getId().equals(uid)) {
                System.out.println(orderList[i].getId() + "\t" + orderList[i].getName() + "\t" + orderList[i].getNumber() + "\t\t" + orderList[i].getPrice() + "\t" + orderList[i].getDate() + "\t" + orderList[i].getSalEpy() + "\t" + orderList[i].getBeiZhu());
            }
        }
        System.out.println();
        String newName = null;
        int newNumber = 0;
        double newPrice = 0;
        String newDate = null;
        String newSalEmp = null;
        String newBeiZhu = null;
        System.out.println("请输入修改后的鲜花名称：");
        newName = INPUT.next();
        //完善功能
        while (true) {
            System.out.println("请输入修改后的销售数量：");
            newNumber = INPUT.nextInt();
            if (newNumber < 0) {
                System.err.println("输入有误！");
                continue;
            } else {
                break;
            }
        }
        while (true) {
            System.out.println("请输入修改后的鲜花价格：");
            newPrice = INPUT.nextDouble();
            if (newPrice < 0) {
                System.err.println("输入有误！");
                continue;
            } else {
                break;
            }
        }
        System.out.println("请输入修改后的销售日期：");
        newDate = INPUT.next();
        System.out.println("请输入修改后的销售员：");
        newSalEmp = INPUT.next();
        System.out.println("请输入修改后的备注：");
        newBeiZhu = INPUT.next();

        System.out.println("您确定要修改吗？（y/n）");
        String confirm = INPUT.next();
        if ("y".equals(confirm)) {
            //创建一个新对象存入修改后的数据
            FlowerOrder order = new FlowerOrder(uid, newName, newNumber, newPrice, newDate, newSalEmp, newBeiZhu);
            //将新对象替换为原来uid的对象
            for (int i = 0; i < orderList.length; i++) {
                if (orderList[i].getId().equals(uid)) {
                    orderList[i] = order;
                }
            }
            System.out.println("修改成功！");
            printHead();
            //输出一遍
            printArray(orderList);
        }

//        for (int i = 0; i < orderList.length; i++) {
//            System.out.println(orderList[i].getId() + "\t" + orderList[i].getName() + "\t" + orderList[i].getNumber() + "\t\t" + orderList[i].getPrice() + "\t" + orderList[i].getDate() + "\t" + orderList[i].getSalEpy() + "\t" + orderList[i].getBeiZhu());
//        }
    }

    //查询所有订单功能
    public static void findAll() {
        //1.打印表头
        printHead();
        //2.遍历数组输出
        printArray(orderList);
    }

    //打印表头功能
    public static void printHead() {
        System.out.println("编号\t\t鲜花名称\t\t\t销售数量\t价格\t\t销售日期\t\t销售员\t备注");
    }

    //遍历输出数组
    public static void printArray(FlowerOrder[] orderLister) {
        for (int i = 0; i < orderList.length; i++) {
            System.out.println(orderList[i].getId() + "\t" + orderList[i].getName() + "\t" + orderList[i].getNumber() + "\t\t" + orderList[i].getPrice() + "\t" + orderList[i].getDate() + "\t" + orderList[i].getSalEpy() + "\t" + orderList[i].getBeiZhu());
        }
    }
}
